package com.alice.generator;

import com.alice.entity.Entity;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyAutoGenerator2 {

    /**
     * 作者
     */
    private static final String AUTHOR="ChenZhengHui";

    /**
     * 模块名称
     */
    private static final String MODULE_NAME = "generator";

    /**
     * 表名
     */
    private static final String[] TAVLES = {"user"};

    /**
     * 名词
     */
    private static final String FUNCTION_NAME = "用户表";

    private static final String JDBC_URL = "jdbc:mysql://139.159.204.92:3306/generator?useUnicode=true&characterEncoding=UTF-8";

    private static final String JDBC_USERNAME = "root";

    private static final String JDBC_PASSWORD = "zh1132";

    private static final String PACKAGE_PARENT = "com.alice";

    public static void main(String[] args) {

        //代码生成器
        AutoGenerator generator = new AutoGenerator();

        //全局变量
        GlobalConfig globalConfig = new GlobalConfig();
        String property = System.getProperty("user.dir");
        globalConfig.setOutputDir(property+"/src/main/java");
        globalConfig.setAuthor(AUTHOR);
        globalConfig.setOpen(false);
        globalConfig.setFileOverride(true);
        globalConfig.setBaseResultMap(true);
        globalConfig.setBaseColumnList(true);
        globalConfig.setSwagger2(true);
        generator.setGlobalConfig(globalConfig);

        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(JDBC_URL);
        dataSourceConfig.setUsername(JDBC_USERNAME);
        dataSourceConfig.setPassword(JDBC_PASSWORD);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        generator.setDataSource(dataSourceConfig);

        //包配置
        PackageConfig packageConfig = new PackageConfig();
//        packageConfig.setModuleName(MODULE_NAME);
        packageConfig.setParent(PACKAGE_PARENT);
        generator.setPackageInfo(packageConfig);


        //自定义设置
        InjectionConfig config = new InjectionConfig(){
            @Override
            public void initMap() {
                Map<String, Object> map = Maps.newHashMap();
                map.put("functionName",FUNCTION_NAME);
                map.put("util","com.alice.util");
                this.setMap(map);
            }
        };

        // 如果模板引擎是 freemarker
//        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";

        //自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig() {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String path = property+"/src/main/resource/mapper/"+tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
                return path;
            }
        });
        config.setFileOutConfigList(focList);
        generator.setCfg(config);

        //配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        generator.setTemplate(templateConfig);

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityTableFieldAnnotationEnable(true);
        strategyConfig.setSuperEntityClass(Entity.class);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setTablePrefix(packageConfig.getModuleName()+"_");
        strategyConfig.setInclude(TAVLES);
        generator.setStrategy(strategyConfig);

        generator.execute();

    }

}
